package org.dtrd.modules.evaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DiabetesInfo;
import org.dtrd.modules.evaluation.entity.dto.DiabetesDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdDiabetes;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdDiabetesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/28-16:16
 */
@Api(tags = "量表评估")
@RestController
public class DiabetesController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdDiabetesService diabetesService;
    @Autowired
    private BaseCommonService baseCommonService;


    @ApiOperation("添加糖尿病风险评估")
    @PostMapping(value = {"/105/26/1052601","/100/17/1001708"})
    public Result<?> saveDiabetesInfo(@RequestBody DiabetesDTO dto) {
        // 参数校验
        if (dto.getDiabetesFamily() == null || dto.getBirth() == null
        || dto.getHeight() == null || dto.getWaistline() == null || dto.getPressureSystolic() == null
        || dto.getGender() == null || dto.getWeight() == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getDiabetesFamily() <= 0 || dto.getHeight() <= 0
                || dto.getWaistline() <= 0 || dto.getPressureSystolic() <= 0
                || dto.getGender() <= 0 || dto.getWeight() <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiabetesInfo diabetesInfo = diabetesService.saveDiabetesInfo(dto);
        String logContent = String.format("添加糖尿病风险评估，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(diabetesInfo);
    }

    @ApiOperation("查询最近一次的糖尿病风险评估-通过patientId")
    @GetMapping("/105/26/1052602")
    public Result<?> getDiabetesInfoLatest(@RequestParam Integer patientId) {
        if (isIntParamInvalid(patientId)) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        DiabetesInfo info = diabetesService.getDiabetesInfoLatest(patientId);
        String logContent = String.format("查询最近一次的糖尿病风险评估，查询id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation("获取糖尿病风险评估记录列表")
    @GetMapping("/105/26/1052603")
    public Result<?> getDiabetesInfoList(@RequestParam Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdDiabetes> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdDiabetes.class)
                .eq(DtrdEntEvaluationRdDiabetes::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdDiabetes::getCreateTime);
        List<DtrdEntEvaluationRdDiabetes> poList = diabetesService.list(queryWrapper);
        List<DiabetesInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new DiabetesInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("获取糖尿病风险评估记录列表，查询id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }

    @ApiOperation("删除糖尿病风险评估记录")
    @DeleteMapping("/105/26/1052604")
    public Result<?> removeDiabetesInfoById(@RequestParam Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = diabetesService.removeById(dataId);
        String logContent = String.format("删除糖尿病风险评估记录，删除记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

}
